package com.sskj.cndragon.presenter;

import com.lzy.okgo.model.Response;
import com.sskj.cndragon.http.HttpConfig;
import com.sskj.cndragon.ui.activity.UserInfoActivity;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.http.JsonCallBack;

import java.io.File;


public class UserInfoActivityPresenter extends BasePresenter<UserInfoActivity> {

    public void uploadImg(String headUrl) {
        httpService.editHeadImg(SPUtil.get(SPConfig.ID, ""), headUrl)
                .tag(this)
                .execute(new JsonCallBack<HttpData>() {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.changeHead(headUrl);
                        } else {
                            ToastUtil.showShort(body.getMsg());
                        }
                    }
                });
    }

    public void uploadFile(File file) {
        httpService.uploadFile(file)
                .tag(this)
                .execute(new JsonCallBack<HttpData<String>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<String>> response) {
                        HttpData body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            uploadImg((String) body.getData());
                        } else {
                            ToastUtil.showShort(body.getMsg());
                        }
                    }
                });
    }
}
